/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Basisofrecord;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Recordlevelelement;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.IllegalOrphanException;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class BasisofrecordJpaController {

    public BasisofrecordJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public Basisofrecord save(Basisofrecord basis) {
        Basisofrecord aux = findBasisofrecord(basis.getBasisofrecord());
        if(aux==null){
            System.out.println("Basis of record "+basis.getBasisofrecord()+" cannot be inserted in database.");
            return null;
        } else
            return aux;
    }
    public void create(Basisofrecord basisofrecord) {
        if (basisofrecord.getRecordlevelelementList() == null) {
            basisofrecord.setRecordlevelelementList(new ArrayList<Recordlevelelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Recordlevelelement> attachedRecordlevelelementList = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListRecordlevelelementToAttach : basisofrecord.getRecordlevelelementList()) {
                recordlevelelementListRecordlevelelementToAttach = em.getReference(recordlevelelementListRecordlevelelementToAttach.getClass(), recordlevelelementListRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementList.add(recordlevelelementListRecordlevelelementToAttach);
            }
            basisofrecord.setRecordlevelelementList(attachedRecordlevelelementList);
            em.merge(basisofrecord);
            for (Recordlevelelement recordlevelelementListRecordlevelelement : basisofrecord.getRecordlevelelementList()) {
                Basisofrecord oldBasisofrecordOfRecordlevelelementListRecordlevelelement = recordlevelelementListRecordlevelelement.getBasisofrecord();
                recordlevelelementListRecordlevelelement.setBasisofrecord(basisofrecord);
                recordlevelelementListRecordlevelelement = em.merge(recordlevelelementListRecordlevelelement);
                if (oldBasisofrecordOfRecordlevelelementListRecordlevelelement != null) {
                    oldBasisofrecordOfRecordlevelelementListRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListRecordlevelelement);
                    oldBasisofrecordOfRecordlevelelementListRecordlevelelement = em.merge(oldBasisofrecordOfRecordlevelelementListRecordlevelelement);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }        
    }

    public void edit(Basisofrecord basisofrecord) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Basisofrecord persistentBasisofrecord = em.find(Basisofrecord.class, basisofrecord.getIdbasisofrecord());
            List<Recordlevelelement> recordlevelelementListOld = persistentBasisofrecord.getRecordlevelelementList();
            List<Recordlevelelement> recordlevelelementListNew = basisofrecord.getRecordlevelelementList();
            List<String> illegalOrphanMessages = null;
            for (Recordlevelelement recordlevelelementListOldRecordlevelelement : recordlevelelementListOld) {
                if (!recordlevelelementListNew.contains(recordlevelelementListOldRecordlevelelement)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recordlevelelement " + recordlevelelementListOldRecordlevelelement + " since its basisofrecord field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Recordlevelelement> attachedRecordlevelelementListNew = new ArrayList<Recordlevelelement>();
            for (Recordlevelelement recordlevelelementListNewRecordlevelelementToAttach : recordlevelelementListNew) {
                recordlevelelementListNewRecordlevelelementToAttach = em.getReference(recordlevelelementListNewRecordlevelelementToAttach.getClass(), recordlevelelementListNewRecordlevelelementToAttach.getIdrecordlevelelement());
                attachedRecordlevelelementListNew.add(recordlevelelementListNewRecordlevelelementToAttach);
            }
            recordlevelelementListNew = attachedRecordlevelelementListNew;
            basisofrecord.setRecordlevelelementList(recordlevelelementListNew);
            basisofrecord = em.merge(basisofrecord);
            for (Recordlevelelement recordlevelelementListNewRecordlevelelement : recordlevelelementListNew) {
                if (!recordlevelelementListOld.contains(recordlevelelementListNewRecordlevelelement)) {
                    Basisofrecord oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement = recordlevelelementListNewRecordlevelelement.getBasisofrecord();
                    recordlevelelementListNewRecordlevelelement.setBasisofrecord(basisofrecord);
                    recordlevelelementListNewRecordlevelelement = em.merge(recordlevelelementListNewRecordlevelelement);
                    if (oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement != null && !oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement.equals(basisofrecord)) {
                        oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement.getRecordlevelelementList().remove(recordlevelelementListNewRecordlevelelement);
                        oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement = em.merge(oldBasisofrecordOfRecordlevelelementListNewRecordlevelelement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = basisofrecord.getIdbasisofrecord();
                if (findBasisofrecord(id) == null) {
                    throw new NonexistentEntityException("The basisofrecord with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Basisofrecord basisofrecord;
            try {
                basisofrecord = em.getReference(Basisofrecord.class, id);
                basisofrecord.getIdbasisofrecord();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The basisofrecord with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Recordlevelelement> recordlevelelementListOrphanCheck = basisofrecord.getRecordlevelelementList();
            for (Recordlevelelement recordlevelelementListOrphanCheckRecordlevelelement : recordlevelelementListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Basisofrecord (" + basisofrecord + ") cannot be destroyed since the Recordlevelelement " + recordlevelelementListOrphanCheckRecordlevelelement + " in its recordlevelelementList field has a non-nullable basisofrecord field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(basisofrecord);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Basisofrecord> findBasisofrecordEntities() {
        return findBasisofrecordEntities(true, -1, -1);
    }

    public List<Basisofrecord> findBasisofrecordEntities(int maxResults, int firstResult) {
        return findBasisofrecordEntities(false, maxResults, firstResult);
    }

    private List<Basisofrecord> findBasisofrecordEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Basisofrecord.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Basisofrecord findBasisofrecord(String q) {
        Query query= getEntityManager().createNamedQuery("Basisofrecord.findByBasisofrecord");
        query.setParameter("basisofrecord",q);

        return query.getResultList().isEmpty()?null:(Basisofrecord)query.getResultList().get(0);
    }
    public Basisofrecord findBasisofrecord(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Basisofrecord.class, id);
        } finally {
            em.close();
        }
    }

    public int getBasisofrecordCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Basisofrecord> rt = cq.from(Basisofrecord.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
